<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form ref="form" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="角色名称">
              <el-input
                v-model="listQuery.role"
                placeholder="角色名称"
                style="width: 200px;"
                class="filter-item"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="角色描述">
              <el-input
                v-model="listQuery.description"
                placeholder="角色描述"
                style="width: 200px;"
                class="filter-item"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="getList">查询</el-button>
        <el-button
          type="primary"
          icon="el-icon-plus"
          class="filter-item"
          @click="addDept"
          v-if="hasPermission('sys:role:add')"
        >
          <span>新增角色</span>
        </el-button>
      </el-form>
    </div>
    <div>
      <h3>角色管理#</h3>
    </div>

    <!--功能点授权-->
    <el-dialog :title="title" class="dialogAdd" width="600px" :visible.sync="dialogAddgsVisible">
      <template>
        <el-input
          v-model="permListQuery.title"
          placeholder="菜单名称"
          style="width: 200px;"
          class="filter-item"
        />
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="getPermList">查询</el-button>
      </template>

      <template>
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="setPerm">授权</el-button>
      </template>

      <!-- <el-table
        :key="tableKey"
        v-loading="listLoading"
        :data="permList"
        border
        fit
        highlight-current-row
        style="width: 100%;margin-top: 15px"
        ref="multipleTable"
        @selection-change="handleSelectionChange"
      >
        <el-table-column prop="permissionId" label="选择" type="selection" fixed></el-table-column>

        <el-table-tree-column
          file-icon="icon icon-file"
          folder-icon="icon icon-folder"
          parentKey="parentId"
          prop="label"
          label="labelname"
          width="220"
        >
          <template slot-scope="{row}">
            <span>{{row.permissionName}}</span>
          </template>
        </el-table-tree-column>

        <el-table-column label="菜单名称" prop="id" align="center" width="200">
          <template slot-scope="{row}">
            <span>{{row.permissionName}}</span>
          </template>
        </el-table-column>

        <el-table-column label="启用" prop="id" align="center" width="100">
          <template slot-scope="{row}">
            <span>{{row.available? "授权":"未授权"}}</span>
          </template>
        </el-table-column>

        <el-table-column label="图标" prop="id" align="center" width="100">
          <template slot-scope="{row}">
            <span>
              <svg-icon :icon-class="row.icon" v-if="row.icon" />
            </span>
          </template>
        </el-table-column>
      </el-table>-->

      <tree-table
        :height="tableHeight"
        :data="permList"
        :columns="columns"
        :root_value="'0'"
        :primary_fld="'permissionId'"
        :expand-all="expandAll"
        :parent_fld="'parentId'"
        :select_fld="'false'"
        ref="treeTable"
      >
        <el-table-column label="类别" min-width="75" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.icon? "菜单权限":"功能权限"}}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" min-width="75" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.available? "已授权":"未授权"}}</span>
          </template>
        </el-table-column>
      </tree-table>
    </el-dialog>

    <!--角色列表-->
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <!--{"id":1,"available":true,"description":"超级管理员","role":"admin","showOrder":""}-->

      <!--<el-table-column label="全选" prop="id" align="center" width="100" >
        <label style="padding-right: 10px;font-size: 15px;font-weight: 500;"><input name="Fruit" type="checkbox"
                                                                                    value=""/> </label>
      </el-table-column>-->
      <el-table-column label="角色名称" prop="id" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{row.role}}</span>
        </template>
      </el-table-column>

      <el-table-column label="角色描述" prop="id" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{row.description}}</span>
        </template>
      </el-table-column>

      <el-table-column label="启用" prop="id" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{row.available? "启用":"未启用"}}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" prop="id" align="center" width="180">
        <template slot-scope="{row}">
          <!--          <el-button type="primary" size="mini" @click="DeptDelete">删除</el-button>-->
          <el-button
            type="primary"
            size="mini"
            @click="menuPermissions(row.id,row.role,row.description)"
          >菜单授权</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getRoles, getRolePerms, getRoleInfo, addRolePerms } from "@/api/role";
import { getPermList } from "@/api/perm";
import Pagination from "@/components/Pagination";
import treeTable from "@/components/treeTable/permTree.vue";

export default {
  name: "InlineEditTable",
  components: { Pagination, treeTable },
  data() {
    return {
      tableKey: 0,
      listLoading: true,
      list: null,
      driver: null,
      listData: null,
      total: 0,
      listQuery: {
        page: 1,
        pageSize: 10
      },

      RoleInfo: {},

      permList: [],
      permListQuery: {},

      addForm: {
        name: "",
        creditCode: "",
        address: "",
        tel: "",
        id: ""
      },
      dialogAddgsVisible: false,
      title: "",

      tableHeight: 500,
      expandAll: true,
      columns: [{ value: "permissionName", text: "菜单", minWidth: "300" }]
    };
  },
  filters: {
    checkStatus: function(s) {
      return s === 1 ? "授权" : "未授权";
    }
  },
  created() {
    this.getList();
  },
  mounted: function() {
    // 监听窗口大小变化
    /* let self = this;
    window.onresize = function() {
      self.tableHeight =
        window.innerHeight -
        self.$refs.treeTable.$el.offsetTop -
        document.getElementsByClassName("navbar")[0].parentNode.clientHeight;
    }; */
  },
  methods: {
    setPerm() {
      /* let dataSet = {
        role: this.permListQuery.roleId,
        list: []
      };

      console.log(this.permListQuery);

      this.multipleSelection.map(item => {
        let perm = {
          permissionId: item.permissionId,
          roleId: dataSet.role
        };
        dataSet.list.push(perm);
      });

      console.log(dataSet); */

      let dataSet = {
        role: 1,
        list: []
      };
      this.permList.map(item => {
        if (item["false"]) {
          let perm = {
            permissionId: item.permissionId,
            roleId: dataSet.role
          };
          dataSet.list.push(perm);
        }
      });
      console.log(dataSet);

      addRolePerms(dataSet).then(data => {
        console.log(data);
        if (data.code === 200) {
          //授权成功
          this.$message("授权成功");
          this.dialogAddgsVisible = false;
          if (this.permListQuery.description === this.$store.getters.roleName) {
            this.open();
          }
        }
      });
    },

    open() {
      this.$confirm("修改了当前用户角色权限, 是否刷新页面?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          window.location.reload();
        })
        .catch(() => {
          /*this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });*/
        });
    },

    /* toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }, */

    addDept() {
      {
        this.dialogAddgsVisible = true;
        this.title = "新增角色";
      }
    },
    getList() {
      console.log(this);
      this.listLoading = true;

      getRoles(this.listQuery).then(response => {
        this.list = response.page.list;
        this.total = response.page.totalCount;
        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false;
        }, 0.5 * 1000);
      });
    },
    menuPermissions(id, role, description) {
      this.dialogAddgsVisible = true;
      this.title = "菜单授权";
      this.permListQuery.roleId = id;
      this.permListQuery.description = description;
      this.getPermList(role);
    },
    getPermList(role) {
      console.log(this);
      this.permListQuery.role = role;
      getRolePerms({ role: role }).then(response => {
        /* this.permList = response.data.sort((a, b) => {
          return a.available - b.available;
        }); */

        this.permList = response.data;

        console.log(this.permList);

        this.permList.map(row => {
          if (row.available === 1) {
            this.$refs.treeTable.chooseNode(row);
          }
        });

        // Just to simulate the time of the request
        setTimeout(() => {
          /* this.permList.forEach(row => {
            if (row.available === 1) {
              this.$refs.multipleTable.toggleRowSelection(row);
            }
          }); */

          this.listLoading = false;
        }, 0.5 * 1000);
      });
    }
  }
};
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}

.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
